<template>
    <div class="container-common admin-common">
        <el-button class="add-user" type="primary" @click="handleAdd">+新增教师</el-button>
        <el-upload
                class="add-user-batch"
                action="http://39.105.144.7:5002/api/user/import"
                name="file"
                :on-success="handleImport"
                :show-file-list="false"
                :data="otherImportData">
            <el-button type="primary">+批量新增</el-button>
            <!--<div slot="tip" class="el-upload__tip">请上传excel文件</div>-->
        </el-upload>
        <el-table
                :data="tableData"
                border>
            <el-table-column
                    prop="id"
                    label="用户ID"
                    width="300">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="用户名">
            </el-table-column>
            <el-table-column
                    prop="password"
                    label="密码">
            </el-table-column>
            <el-table-column
                    prop="phone"
                    label="电话">
            </el-table-column>
            <el-table-column
                    prop="email"
                    label="邮箱">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="160">
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="large"
                            @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
                title="添加教师"
                :visible.sync="addDialogVisible"
                width="50%">
            <el-form :model="addForm" ref="addForm" label-width="60px" class="add-form">
                <el-form-item label="学号" prop="id">
                    <el-input v-model="addForm.id"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name"></el-input>
                </el-form-item>
                <el-form-item label="电话" prop="phone">
                    <el-input v-model="addForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelAdd">取 消</el-button>
                <el-button type="primary" @click="confirmAdd">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog
                title="删除教师"
                :visible.sync="deleteDialogVisible"
                width="30%">
            <p v-if="currentData">确认删除教师{{currentData.id}}？</p>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelDelete">取 消</el-button>
                <el-button type="primary" @click="confirmDelete">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import Api from '@utils/api';

    export default {
        name: "manageTeacher",
        data () {
            return {
                tableData: [],
                currentData: null,
                deleteDialogVisible: false,
                addDialogVisible: false,
                addForm: {
                    id: '',
                    name: '',
                    phone: '',
                    email: ''
                }
            }
        },
        computed: {
            otherImportData () {
                return {
                    role_name: '教师'
                }
            }
        },
        methods: {
            handleAdd () {
                this.addDialogVisible = true;
            },
            handleDelete (data) {
                this.currentData = data;
                this.deleteDialogVisible = true;
            },
            cancelAdd () {
                this.addDialogVisible = false;
                this.resetAdd();
            },
            confirmAdd () {
                Api({
                    method: 'post',
                    url: '/addUser',
                    data: {
                        User_id: this.addForm.id,
                        user_name: this.addForm.name,
                        role : 'Teacher',
                        email: this.addForm.email,
                        phone: this.addForm.phone
                    }
                }).then(() => {
                    this.$notify({
                        title: "添加成功！",
                        type: "success"
                    });
                    this.addDialogVisible = false;
                    this.resetAdd();
                    this.getTableData();
                })
            },
            resetAdd () {
                this.addForm = {
                    id: '',
                    name: '',
                    phone: '',
                    email: ''
                };
            },
            cancelDelete () {
                this.deleteDialogVisible = false;
            },
            confirmDelete () {
                Api({
                    method: 'get',
                    url: '/deleteUser',
                    params: {
                        user_id: this.currentData.id
                    }
                }).then(() => {
                    this.$notify({
                        title: "删除成功！",
                        type: "success"
                    });
                    this.deleteDialogVisible = false;
                    this.getTableData();
                })
            },
            getTableData () {
                Api({
                    method: 'get',
                    url: '/getUserList',
                    params: {
                        role_name: "Teacher"
                    }
                }).then(res => {
                    if (res.data && res.data.data && res.data.data.length) {
                        this.tableData = res.data.data.map(item => {
                            return {
                                id: item.user_id,
                                name: item.user_name,
                                password: item.password,
                                phone: item.phone || '',
                                email: item.email || ''
                            }
                        });
                    }
                })
            },
            handleImport () {
                this.$notify({
                    title: '录入成功！',
                    type: 'success'
                });
                this.getTableData();
            }
        },
        mounted () {
            this.getTableData();
        }
    }
</script>

<style type="text/scss" lang="scss">
    .admin-common {
        .add-user {
            margin-bottom: 20px;
            margin-right: 20px;
            vertical-align: top;
        }
        .add-user-batch {
            display: inline-block;
        }
    }
</style>